import 'package:flutter/material.dart';
import "package:eventsource/eventsource.dart";
import 'dart:convert';

void main() async {
  runApp(SampleApp());
}

class SampleApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Sample App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  List<Map<String, dynamic>> listOfColumns = [];
  bool _pause = false;
  EventSource eventSource;

  void _onChanged(bool value) => setState(() => _pause = value);

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  _loadData() async {
    eventSource = await EventSource.connect("http://10.213.20.43:9910/msi/mainTableInfo");

    // listen for events
//    if (!_pause) {
      eventSource.listen((Event event) {
        setState(() {
          String data = "${event.data}";
          print(data);
          print(json.decode(data));
          print("Pause: $_pause");
          if (!_pause) {
            listOfColumns.add(json.decode(data));
            print(listOfColumns.length);
          }
        });
      });
//    }
  }

  var latestItemTextStyle = TextStyle(
//      fontSize: 32.0,
      color: Colors.red[900],
      fontWeight:FontWeight.w600
  );

  DataRow _getDataRow(index) {
    if (index == listOfColumns.length - 1) { // The latest item with special style.
      return DataRow(
        cells: <DataCell>[
          DataCell(Text(listOfColumns[index]["fileName"], style: latestItemTextStyle,)),
          DataCell(Text(listOfColumns[index]["startTime"], style: latestItemTextStyle,)),
          DataCell(Text(listOfColumns[index]["endTime"], style: latestItemTextStyle,)),
          DataCell(Text(listOfColumns[index]["status"], style: latestItemTextStyle,)),
        ],
      );
    } else {
      return DataRow(
        cells: <DataCell>[
          DataCell(Text(listOfColumns[index]["fileName"])),
          DataCell(Text(listOfColumns[index]["startTime"])),
          DataCell(Text(listOfColumns[index]["endTime"])),
          DataCell(Text(listOfColumns[index]["status"])),
        ],
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('文件列表'),
        actions: <Widget>[
          new Center(
            child: new Text('ON',
                style: new TextStyle(
                    fontWeight: FontWeight.bold,
                    color: Colors.greenAccent)
            ),
          ),
          new Switch(
            value: _pause,
            onChanged: _onChanged,
            activeTrackColor: Colors.blueGrey,
            activeColor: Colors.grey,
          ),
          new Center(
            child: new Text('OFF',
                style: new TextStyle(
                    fontWeight: FontWeight.bold,
                    color: Colors.grey)
            ),
          ),
          new Center(
            child: new Padding(padding: EdgeInsets.all(20.0)),
          ),
        ],
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: new ListView(
          children: <Widget>[
            new DataTable(
              columns: [
                DataColumn(label: Text('File')),
                DataColumn(label: Text('Start')),
                DataColumn(label: Text('End')),
                DataColumn(label: Text('Status')),
              ],
              rows: List.generate(listOfColumns.length,
                      (index) => _getDataRow(listOfColumns.length - 1 - index)
              ),
            )
          ],
        ),
      ),
    );
  }
}
